package com.alstudio.ui.module.setting;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.alstudio.app.ALLocalEnv;
import com.alstudio.event.ALEventDeliver;
import com.alstudio.module.xmpp.protocol.xmpp.data.ALXmppEventType;
import com.alstudio.module.xmpp.request.ALUiXmppRequestCreator;
import com.alstudio.pojo.ALXmppEvent;
import com.alstudio.ui.base.TitleBarActivity;
import com.alstudio.ui.module.account.GetAccountActivity;
import com.alstudio.ui.module.recharge.RechargeActivity;
import com.alstudio.ui.module.voip.XieHouActivity;
import com.alstudio.ui.module.web.ALWebViewManager;
import com.alstudio.utils.android.AndroidUtils;
import com.alstudio.utils.android.color.ALColorUtils;
import com.alstudio.utils.notification.SystemNotificationEnvConfig;
import com.alstudio.view.dialog.AlertDialogCreator;
import com.alstudio.view.listener.AlDialogOnClickListener;
import com.alstudio.view.tableview.ALTableView;
import com.alstudio.view.tableview.ALTableView.ALTableViewClickListener;
import com.alstudio.view.tableview.BasicItem;
import com.alstudio.view.toast.MyToastUtil;
import com.loovee.imaohu.R;

public class SystemSettingActivity extends TitleBarActivity implements
		ALTableViewClickListener {

	private ALTableView tableView;
	private ArrayList<Intent> intents = new ArrayList<Intent>();
	// 跳转到通知设置
	private final int GOTO_NOTICE_SETTING = 0;
	// 跳转到隐私设置
	private final int GOTO_PRAVICY_SETTING = 1;
	// 跳转到意见反馈
	private final int GOTO_FEED_BACK = 2;
	// 跳转到帮助
	private final int GOTO_HELP = 3;
	// 评价软件
	private final int GOTO_PRIASE_APP = 4;
	// 关于软件
	private final int GOTO_ABOUT_APP = 5;
	// 清除缓存
	private final int CLEAR_CACHE = 6;
	// 退出登录
	private final int LOG_OUT = 7;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setupView(this, getClass().getSimpleName());
	}

	@Override
	protected void setupView(Context context, String name) {
		// TODO Auto-generated method stub
		super.setupView(context, name);
		// 设置本界面的UI
		setTitleBarContentView(R.layout.system_setting_layout);
		hideRightButton(true);
		setCenterTextViewMessage(R.string.TxtMainSettingTitle);
		tableView = (ALTableView) findViewById(R.id.aLTableView1);
		initTableView();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.ui.base.BaseActivity#finish()
	 */
	@Override
	public void finish() {
		// TODO Auto-generated method stub
		super.finish();
		intents.clear();
	}

	/**
	 * 初始化列表
	 */
	private void initTableView() {

		tableView.clear();
		intents.clear();

		// 通知
		BasicItem item = new BasicItem(getString(R.string.TxtNotice));
		item.setDrawable(R.drawable.icon_system_setting);
		tableView.addBasicItem(item);

		// 隐私
		item = new BasicItem((getString(R.string.TxtPriavcy)));
		item.setDrawable(R.drawable.icon_system_setting);
		tableView.addBasicItem(item);

		// 加入分段
		tableView.addSegmentations(R.layout.default_blank_list_segmentation);

		// 意見反饋
		item = new BasicItem(getString(R.string.TxtFeedBackTitle));
		item.setDrawable(R.drawable.icon_system_setting);
		tableView.addBasicItem(item);

		// 帮助
		item = new BasicItem(getString(R.string.TxtMainSettingUserGuideTitle));
		item.setDrawable(R.drawable.icon_system_setting);
		tableView.addBasicItem(item);

		// 评价软件
		item = new BasicItem(getString(R.string.TxtPraiseApp));
		item.setDrawable(R.drawable.icon_system_setting);
		tableView.addBasicItem(item);

		// 关于软件
		item = new BasicItem(getString(R.string.TxtAboutApp));
		item.setDrawable(R.drawable.icon_system_setting);
		item.setItemCountColor(ALColorUtils.getColor(getApplicationContext(),
				R.color.white));
		if (ALLocalEnv.getLocalEnv().isNewVersionAlert()) {
			item.setCountNum("new");
			item.setItemCountDrawable(R.drawable.friend_mutual_icon_massage);
		} else {
			item.setCountNum("");
		}
		tableView.addBasicItem(item);

		// 加入分段

		tableView.addSegmentations(R.layout.default_blank_list_segmentation);
		// 清除缓存
		item = new BasicItem(getString(R.string.TxtCleanCache));
		item.setDrawable(R.drawable.icon_system_setting);
		tableView.addBasicItem(item);

		// 加入分段
		tableView.addSegmentations(R.layout.default_blank_list_segmentation);

		// 退出登录
		item = new BasicItem(getString(R.string.TxtLogout));
		item.setDrawable(R.drawable.icon_system_setting);
		tableView.addBasicItem(item);

		tableView.setClickListener(this);
		tableView.commit();

	}

	@Override
	public void onClick(int index) {
		// TODO Auto-generated method stub
		invokeItemClick(index);
	}

	private void invokeItemClick(int index) {
		Intent i = null;
		boolean isFinishRequest = false;
		switch (index) {
		case GOTO_NOTICE_SETTING:
			// 跳转到通知设定
			i = new Intent(this, NoticeSettingActivity.class);
			break;
		case GOTO_PRAVICY_SETTING:
			i = new Intent(this, PrivacySettingActivity.class);
			break;
		case GOTO_FEED_BACK:
			i = new Intent(this, FeedBackActivity.class);
			break;
		case GOTO_HELP:
			ALWebViewManager.startWebActivity("http://www.baidu.com");
			break;
		case GOTO_PRIASE_APP:
			AndroidUtils.rateApplication(this);
			return;
		case GOTO_ABOUT_APP:
			i = new Intent(this, AboutActivity.class);
			break;
		case CLEAR_CACHE:
			MyToastUtil.getInstance().showCenterToastOnCenter(
					R.string.TxtClearMemoryCacheSuccessed);
			new Thread() {
				public void run() {
					ALLocalEnv.getLocalEnv().clearImageLoaderDiskCache();
				};
			}.start();
			return;
		case LOG_OUT:
			showLogOutDialog();
			break;

		}
		if (i != null) {
			startActivity(i);
			if (isFinishRequest) {
				finish();
			}
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		super.onClick(v);
	}

	@Override
	protected void initData() {
		// TODO Auto-generated method stub

	}

	private void showLogOutDialog() {
		final AlertDialogCreator mDialogCreator = AlertDialogCreator
				.createTwoButtonNormalDialog(this, false);
		mDialogCreator.setLeftKeyListener(new AlDialogOnClickListener() {

			@Override
			public void onClick() {
				ALUiXmppRequestCreator.postALUiXmppLogOutEvent();
				ALLocalEnv.getLocalEnv().postFinishAllActivityEvent();
				ALLocalEnv.getLocalEnv().clearEnv();
				Intent i = new Intent(SystemSettingActivity.this,
						GetAccountActivity.class);
				startActivity(i);
			}
		});

		mDialogCreator.setMessage(getString(R.string.TxtLogoutHint));

		mDialogCreator.showDialog();
	}

}
